package com.xwj.config;

import com.xwj.Handler.RedisCacheContextClosedHandler;
import com.xwj.Handler.ThreadPoolContextClosedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

/**
 * ConfigurableApplicationContext通用监听器
 */

@Component
public class CustomContextClosedHandlerConfig implements ApplicationListener<ApplicationEvent> {

    @Autowired
    private ThreadPoolContextClosedHandler threadPoolContextClosedHandler;
    @Autowired
    private RedisCacheContextClosedHandler redisCacheContextClosedHandler;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextClosedEvent) {//当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发
            threadPoolContextClosedHandler.onApplicationEvent();
            redisCacheContextClosedHandler.clearAllCache();
        } else if (event instanceof ContextRefreshedEvent) {//ApplicationContext容器初始化或者刷新时触发

        } else if (event instanceof ContextStartedEvent) {//当使用ConfigurableApplicationContext接口的start()方法启动ApplicationContext容器时触发

        } else if (event instanceof ContextStoppedEvent) {//当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发

        } else {

        }
    }
}
